<template>
	<view class="cart_box" v-if="buffer">
		<view class="cart_head">
			<view class="cart_menu">
				<text>全部(20)</text>
				<text>降价(4)</text>
				<text>常卖(2)</text>
				<text>分类</text>
			</view>
			<view class="cart_ticket">
				<!-- 此处使用checkbox是因为支付宝switch没有checkbox类型 -->
				<!-- <switch type="checkbox" :checked="true" color="#ff6040"  /> -->
				<checkbox color="#FF6040" value="true" />
				<text class="cart_ticket_text1">小U自营</text>
				<text class="cart_ticket_text2">以免运费 |</text>
				<text class="cart_ticket_text3">领券</text>
			</view>
		</view>

		<view class="container">
			<view class="top">
				<scroll-view class="scroll-view_H" scroll-x scroll-left="0" v-for="(item,index) in cartList"
					:key="item.id">
					<view class="cartInfo" id="demo1">
						<!-- 明日来写购物车基本信息 -->
						<view class="cartInfochild">
							<!-- 选择框 -->
							<view class="cartInfo_switch common">
								<!-- 点击当前商品 -->
								<checkbox-group>
									<checkbox color="#FF6040" :checked="item.status" @click="changeOne(index)" />
								</checkbox-group>

								<!-- <switch type="checkbox" :checked="true"  color="#ff6040" /> -->
							</view>
							<!-- 图片 -->
							<view class="cartInfo_image common">
								<image :src="item.img" mode="widthFix"></image>
							</view>

							<!-- 信息 -->
							<view class="cartInfo_info">
								<label style="font-size: 32rpx;">{{item.goodsname}}</label>
								<label style="font-size: 24rpx; color: #ccc;">规格：不错的商品</label>
								<label style="font-size: 20rpx; color: #FF6040;">比加入降低￥100</label>
								<label style="color:#FF6040;">￥
									<text style="font-size: 40rpx;color:#FF6040;">{{item.price}}
									</text> <text>￥{{item.market_price}}</text>
								</label>
							</view>
							<!-- 加减数量 -->
							<view class="cartInfo_num">
								<view class="cartInfo_num_child">
									<label @click="changenum(1,index,item.id)">－</label>
									<label>{{item.num}}</label>
									<label @click="changenum(2,index,item.id)">＋</label>
								</view>
							</view>
						</view>

					</view>
					<view class="cartDel" id="demo2" @click="cartDelete(index,item.id)">
						<label for="">删除</label>
					</view>
				</scroll-view>
			</view>

			<!-- 底部 -->
			<view class="foot">
				<view class="footd1">
					<checkbox-group @change="changeAll">
						<checkbox color="#FF6040" :checked="allStatus" />
					</checkbox-group>
					<!-- <switch type="checkbox" :checked="true" color="#ff6040" /> -->
					<label>全选</label>
				</view>
				<view class="footd2">
					<view class="footd2sp1">合计：<label style="color: #FF6040;">￥{{jisuan.countmoney}} </label></view>
					<label class="footd2sp2">不含运费，已优惠￥0.00</label>
				</view>
				<!-- 跳到提交订单，结算页面 -->
				<view class="footd3" hover-class="touchOpacity" @click="goOrderConfirm">
					<!-- 被选中的商品的个数 -->
					<label>去结算({{jisuan.countnum}})件</label>
				</view>
			</view>

		</view>
	</view>
	<!-- 无数据提示 -->
	<view v-else>
		<cartnull />
	</view>
</template>

<script>
	// 引入数据为空提示组件
	import cartnull from '@/components/cartnull/cartnull.vue';
	export default {
		components: {
			cartnull
		},
		data() {
			return {
				cartList: [], //初始化购物车数据
				buffer: true, //检测购物车是否有数据
				allStatus: true
			}
		},
		// 页面显示时
		onShow() {
			let userInfo = this.$tool.islogin();
			let uid = userInfo.uid
			if (userInfo != false) {
				this.getcartlist({
					uid
				})
			}

		},
		computed: {
			jisuan() {
				let countmoney = 0
				let countnum = 0
				this.cartList.forEach(item => {
					if (item.status) {
						countmoney += parseFloat(item.price) * parseInt(item.num)
						countnum += parseInt(item.num)
					}
				})
				return {
					countmoney,
					countnum
				}
			}
		},
		methods: {
			goOrderConfirm() {
				let order = [];
				this.cartList.forEach(item => {
					if (item.status) {
						order.push(item)
					}
				})
				console.log(order.length)
				if (order.length == 0) {
					this.$tool.tip('请勾选商品！');
					return;
				}
				uni.setStorageSync('orders',{order,...this.jisuan})
				uni.navigateTo({
					url: '../order/order'
				})
			},
			cartDelete(index, id) {
				uni.showModal({
					title: "温馨提示",
					content: "删除后不可恢复！",
					success: res => {
						if (res.confirm) {
							// 2.删除页面节点
							this.cartList.splice(index, 1)
							// 3.同步数据库
							this.$api.reqcartdelete({
									id,
								})
								.then(res => {
									// 4.检测是否还有数据没有就提示为空
									if (this.cartList.length == 0) {
										this.buffer = false
									}
								})
								.catch(err => console.log(err))
						}
					}
				})
			},
			changenum(type, index, id) {
				if (type == 1) {
					if (this.cartList[index].num == 1) {
						this.$tool.tip("该宝贝不能再减少了哟~")
						// console.log(this.cartList[index].num)
						return;
					}
					this.cartList[index].num--
					// console.log(this.cartList[index].num)
				} else {
					this.cartList[index].num++
				}
				if (type == 1 && this.cartList[index].num > 0 || type == 2) {
					this.$api.reqcartedit({
							id,
							type
						})
						.then(res => console.log(res))
						.catch(err => console.log(err))
				}
				// console.log(this.cartList[index].num)

			},
			changeAll() {
				this.allStatus = !this.allStatus
				this.cartList.forEach(item => item.status = this.allStatus)
			},
			changeOne(index) {
				console.log(index)
				this.cartList[index].status = !this.cartList[index].status
				this.allStatus = this.cartList.every(item => item.status)
			},
			getcartlist(data) {
				this.$api.reqcartlist(data)
					.then(res => {
						let cartdata = res.list
						cartdata.forEach(item => {
							item.status = true; //重置购物车商品状态为bool
							item.img = this.$URL + item.img
						})
						this.cartList = cartdata
						console.log(cartdata)
					}).catch(err => console.log(err))
			}
		}
	}
</script>

<style scoped>
	/* 引入样式文件 */
	@import url("@/static/css/cart.css");
</style>
